package com.fy.fyzf.dialog;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.zhouwei.library.CustomPopWindow;
import com.fy.fyzf.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.List;

public class SelectHousingPopwindow {

    private List<String> titleList;
    private List<Integer> idList;
    private Context mContext;
    private String title;
    private final View popView;
    private CustomPopWindow customPopWindow;

    public SelectHousingPopwindow(Context context) {
        mContext = context;
        popView = LayoutInflater.from(mContext).inflate(R.layout.popwindow_loop_view, null, false);

    }

    public SelectHousingPopwindow setTitleData(List<String> list) {
        titleList = list;
        return this;
    }

    public SelectHousingPopwindow setTitle(String s) {
        title = s;
        return this;
    }

    public SelectHousingPopwindow setIds(List<Integer> list) {
        idList = list;
        return this;
    }

    private Integer selectValue = null;

    public Integer getSelectValue() {
        return position;
    }

    private int position = 0;

    public View getView(int i) {
        return popView.findViewById(i);
    }

    public void dismiss() {
        if (null != customPopWindow) {
            customPopWindow.dissmiss();
        }
    }

    public void showPop(View view) {
        customPopWindow = new CustomPopWindow.PopupWindowBuilder(mContext).setView(popView)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, 600)
                .setFocusable(true)//是否获取焦点，默认为ture
                .setOutsideTouchable(false)//是否PopupWindow 以外触摸dissmiss
                .enableBackgroundDark(true)
                .create()//创建PopupWindow
                .showAtLocation(view, Gravity.BOTTOM, 0, 0);

        LoopView loopView = popView.findViewById(R.id.loopView);
        TextView tvTitle = popView.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        loopView.setTextSize(16);
        loopView.setInitPosition(0);
        loopView.setItems(titleList);
        selectValue = idList.get(position);
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.e("onItemSelected", "" + index);
                position = index;
                selectValue = idList.get(position);
            }
        });

        popView.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customPopWindow.dissmiss();

            }
        });

        /*popView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectValue = idList.get(position);
                customPopWindow.dissmiss();
            }
        });*/
    }

}
